<?php

class Admin_AclPermissionController extends Custom_Controller_Action
{
	private $_resources = '';

    public function init()
    {
        /* Initialize action controller here */
		$resources = $this->_helper->AssetsList->getList();
		$this->_resources = $resources;

		$this->_setTab();
    }

    public function indexAction()
    {
        if($this->isAjax)
		{
			$fetch = $this->_getParam('f');
			$objResource	 = new User_Model_DbTable_AclResource();
			$objPermission  = new User_Model_DbTable_AclPermission();
			
			switch($fetch)
			{
				case 'rs':

						$module		 = $this->_getParam('m');
						$data['resources'] = $objResource->getByResource(array_keys($this->_resources[$module]));
						
						if($data['resources'])
						{
							$data['permissions'] = $objPermission->getPermissions($data['resources'][0]['id'],$this->_resources[$module][$data['resources'][0]['resource']]);
						}
						echo Custom_General::toJSON($data);

					break;
				case 'per':

						$formData['module']	 = $this->_getParam('m');
						$formData['resource'] = $this->_getParam('r');
						
						if($formData['resource'])
							$this->_showPermissions($formData);
						else
							echo false;
						
					break;
				default:
					break;
			}

			exit;
		}
		else
		{
			$modules = array_keys($this->_resources);
			#unset admin module
			unset ($modules[0]);
			$this->view->modules = $modules;
		}
    }

    public function addAction()
    {
		$objResource	 = new User_Model_DbTable_AclResource();
		$objPermission  = new User_Model_DbTable_AclPermission();

        if($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
			
			if($formData['available'] && $formData['resource'])
			{
				foreach($formData['available'] as $value)
				{
					$data['resource_id'] = $formData['resource'];
					$data['permission']  = $value;
					$data['role_id']	= User_Model_DbTable_Role::REGISTER_USER;
					$objPermission->insert($data);
				}
			}

			if($this->isAjax)
			{
				if($formData['resource'])
					$this->_showPermissions($formData);
				else
					echo false;

				exit;
			}
		}
    }

    public function deleteAction()
    {
        $objResource	= new User_Model_DbTable_AclResource();
		$objPermission  = new User_Model_DbTable_AclPermission();

        if($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();

			if($formData['assigned'])
			{
				foreach($formData['assigned'] as $value)
				{
					$objPermission->deleteById($value);
				}
			}

			if($this->isAjax)
			{
				if($formData['resource'])
					$this->_showPermissions($formData);
				else
					echo false;

				exit;
			}
		}
    }

	private function _showPermissions($args)
	{
		$objResource	 = new User_Model_DbTable_AclResource();
		$objPermission  = new User_Model_DbTable_AclPermission();
		
		$module	  = $args['module'];
		$resource = $objResource->getOne($args['resource']);
		$data['permissions'] = $objPermission->getPermissions($resource['id'],$this->_resources[$module][$resource['resource']]);
		echo Custom_General::toJSON($data);
	}

	private function _setTab()
	{
		$subItems = array(__('ACL Permission'));
		$subItemLinks = array('admin/aclpermission');

		$subMenues = array('items' => $subItems, 'links'=> $subItemLinks,
						   'enableMenues' => array('0'), 'activeIndx' => 0);

		$this->view->subMenues = $subMenues;
	}
}







